package hydrap2p.proxy.manager;

import hydrap2p.logging.Level;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ManagerConnectionListener implements Runnable {

	private ServerSocket listener;
	private boolean running;
	
	public ManagerConnectionListener(int port) throws IOException
	{
		listener=new ServerSocket(port);
		ProxyLogging.log(Level.DEBUG, "ProxyManager - ManagerConnectionListener", "Listening for peer connections at "+port);
		
	}
	
	@Override
	public void run()
	{
		Socket clientSocket=null;
		running=true;
		try {
			while (running)
			{
				clientSocket=listener.accept();
				ProxyLogging.log(Level.DEBUG, "ProxyManager - ManagerConnectionListener", "Accepting new proxy manager connection from "+clientSocket.getInetAddress()+":"+clientSocket.getPort());
				ManagerConnectionProcessor processor=new ManagerConnectionProcessor(clientSocket);
				new Thread(processor,processor.toString()).start();
			}
		}
		catch (IOException e) {
			
		}
	}
	
	@Override
	public String toString()
	{
		return "ManagerConnectionListener "+listener.getLocalPort();
	}
	
}
